from rest_framework import serializers

from users.models import User
from .models import Category, News, Comment

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'avatar')

class CommentSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    class Meta:
        model = Comment
        fields = ('id', 'content', 'count')

class SubCommentSerializer(serializers.ModelSerializer):
    parent = CommentSerializer(many=True, read_only=True)
    class Meta:
        model = Comment
        fields = ('id', 'content', 'count', 'parent')


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('id', 'name')

class NewsDetailSerializer(serializers.ModelSerializer):
    user = serializers.SlugRelatedField(label='作者', read_only=True, slug_field='username')
    # comment = CommentSerializer()
    class Meta:
        model = News
        fields = ('id', 'title', 'create_time', 'content',
                  'status', 'is_collect')

class NewsListSerializer(serializers.ModelSerializer):
    user = serializers.SlugRelatedField(label='作者', read_only=True, slug_field='username')
    class Meta:
        model = News
        fields = ('id', 'title', 'create_time', 'digest', 'image',
                  'status', 'is_follow')